<!--#include file="Enumerable.asp" -->
<%	
	var $System={template:{}};
		$System.template.path='$Plate';	
	var ALLELUIA={};
		/* 视图模型 */
		ALLELUIA.view=Class.create();
		ALLELUIA.view.prototype=
		{
			initialize:function($options)
			{
				try
				{
					$options=($options||{});

					this.path=($options.path||'');									
					this.charset=($options.charset||'UTF-8');
					this.data=($options.data||{common:undefined,module:undefined});
					this.template={main:'',module:{}};			

					if(this.path=='')
					{
						if(typeof($System)!='undefined')
						{
							try
							{
								var tempScriptName=Request.ServerVariables("SCRIPT_NAME").item.split('/');
									tempScriptName=tempScriptName[tempScriptName.length-1];
									tempScriptName='\\'+tempScriptName;								
								this.path=Server.MapPath($System.template.path+tempScriptName);
								if(FSO.fileExists(this.path)==false){
									throw 'Path Error 2';
								}
							}
							catch(e)
							{
								throw 'Path Error 1';
							}
						}
						else
						{
							throw 'Path Error 0';
						}
					}
					
					
					//读取模版
					var templateStream=Server.CreateObject('ADODB.Stream');
						templateStream.charset=this.charset;
						templateStream.type=adTypeText;
						templateStream.mode=adModeReadWrite;
						templateStream.Open();

						templateStream.loadFromFile(this.path);						
						this.template.main=templateStream.readText();
						templateStream.Close(),templateStream=null;
						
						var moduleLable=this.template.main.match(/<!--\[module name=\"(\w*)\"\]-->/ig);
						var moduleName=[];
						if(moduleLable!=null)
						{
							moduleLable.map
							(
								function($v,$i)
								{
									moduleName.push($v.match(/name=\"(\w*)\"/)[1]);
								}
							);							
						}

						moduleName.map
						(
							function($v,$i)
							{
								var tempRegexp=new RegExp('<!--\\[module name=\\"'+$v+'\\"\\]-->([\\w\\W]*?)<!--\\[\\/module\\]-->'); 
								try
								{
									this.template.module[$v]=(this.template.main.match(tempRegexp)[1]);								
									//echo(this.template.main.match(tempRegexp)[1])
								}catch(e){
									echo('error:'+$v)
								}								
							},
							this
						);					
				}
				catch(e)
				{
					if(e=='Connection error')
						throw new Error(cusErrors_0020_ADO$Command);
					else if(e=='Commandtext error')						
						throw new Error(cusErrors_0021_ADO$Command);
					else 
					{
						if(e!=cusFrameworkDo$Break&&Object.varType(e)!='string')							
							throw e;
						else
							throw new Error(e);
					}					
				}
			},
			render:function($data)
			{			
				if($data!=undefined) this.data=$data;

				//#模块值
				if(this.data.module!=undefined)
				{					
					var moduleData=this.data.module;
					for(var imodule in moduleData)
					{
						if(imodule!='extend')
						{
							var tempText='',tempObject={};
							var tempData=moduleData[imodule];
							if(this.template.module[imodule]!=undefined)
							{
								var tempTemplate=new Template(this.template.module[imodule]);
								
								//计算行数
								var rowCount=0;
								for(var iColumn in tempData)
								{
									if(iColumn!='extend')
									{
										if(tempData[iColumn]!=undefined){
											rowCount=tempData[iColumn].length;
											break;
										}
									}
								}								
								
								//拼模块数据
								for(var iRow=0;iRow<rowCount;iRow++)
								{
									for(var iColumn in tempData)
									{
										tempObject[iColumn]=tempData[iColumn][iRow];
									}
									tempText+=tempTemplate.evaluate(tempObject);
								}								
								tempTemplate=null
	
								//替换模版
								//if(tempText=='') tempText='暂无该信息。';
								var moduleRegexp=new RegExp('<!--\\[module name=\\"'+imodule+'\\"\\]-->([\\w\\W]*?)<!--\\[\\/module\\]-->','ig');
								this.template.main=this.template.main.replace(moduleRegexp,tempText);
								
								tempText='';
							}
						}
					}
				}
				
				//#标量值
				if(this.data.common!=undefined)
				{
					var pageTemplate=new Template(this.template.main);
						this.template.main=pageTemplate.evaluate(this.data.common);
						pageTemplate=null;
				}
				
				//#页面值
				if(this.data.refPage!=undefined)
				{					
					for(var iRef in this.data.refPage)
					{
						var tempValue=iRef.replace(/\$_\$/,'.');
						var refPageRegexp=new RegExp('\\<\\!--\\s?#include\\s?file=\\"'+tempValue+'\\"\\s?--\\>','ig');												
						if(iRef!='extend')
						{							
							this.template.main=(this.template.main.replace(refPageRegexp,this.data.refPage[iRef]));
						}
					}
				}
				
				return this.template.main;
			},
			close:function()
			{
				Object.clear(this);
			}
		};
		ALLELUIA.view.get=function($path,$data)
		{			
			$path=($path.match(/\:/)) ? $path:Server.MapPath($System.template.path+'/'+$path);
			//doc($path),over()
			var tempValue='';
			var viewObject=new ALLELUIA.view({data:$data,path:$path});
				tempValue=viewObject.render();
				viewObject.close(),viewObject=null;				
			return tempValue;
		};
		
		/* 数据集对象 */
		ALLELUIA.dataSet=Recordset;
		

%>